<template>
  <div class="eadd">
    <el-card class="box-card">
      <el-button
        class="submission priamry_border"
        style="display: flex; float: right"
        @click="back()"
        >返回</el-button
      >
      <div class="title">{{ roomid ? "编辑房型" : "添加房型" }}</div>
      <!-- 选择酒店 -->
      <div v-if="!this.$route.params.id">
        <span style="font-weight:700; font-size:14px; margin-right:4px"
          >请选择酒店名称：</span
        >
        <el-select
          v-model="roominfolist.jdId"
          placeholder="请选择酒店名称"
          @change="seletjd($event)"
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.jdName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
      <div style="margin: 15px 0"><hr /></div>
      <!-- 表单 -->
      <el-form
        ref="formValidate"
        class="frame"
        :inline="true"
        :rules="rules"
        :model="roominfolist"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="房间类型：" prop="fjType">
              <el-input
                v-model="roominfolist.fjType"
                maxlength="120"
                placeholder="请输入房间类型"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="饮品：" prop="drink">
              <el-input
                v-model="roominfolist.drink"
                maxlength="120"
                placeholder="请输入饮品"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房间设施：" prop="facility">
              <el-input
                v-model="roominfolist.facility"
                maxlength="120"
                placeholder="请输入房间设施"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="房间面积：" prop="fjArea">
              <el-input
                v-model="roominfolist.fjArea"
                maxlength="120"
                placeholder="请输入房间面积"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="商品床型：" prop="fjBedType">
              <el-input
                v-model="roominfolist.fjBedType"
                maxlength="120"
                placeholder="请输入床型"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="卫生间信息：" prop="fjToilet">
              <el-input
                v-model="roominfolist.fjToilet"
                maxlength="120"
                placeholder="请输入房间卫生间信息"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="房型名称：" prop="fxName">
              <el-input
                v-model="roominfolist.fxName"
                maxlength="120"
                placeholder="请输入房型名称"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="房间窗户信息：" prop="fjWindows">
              <el-input
                v-model="roominfolist.fjWindows"
                maxlength="120"
                placeholder="请输入房间窗户信息"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="容纳人数：" prop="galleryful">
              <el-input
                v-model="roominfolist.galleryful"
                maxlength="120"
                placeholder="请输入容纳人数"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="娱乐设备：" prop="recreation">
              <el-input
                v-model="roominfolist.recreation"
                maxlength="120"
                placeholder="请输入娱乐设备"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="洗漱用品：" prop="wash">
              <el-input
                v-model="roominfolist.wash"
                maxlength="120"
                placeholder="请输入洗漱用品"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <div class="fjBed">
              <span>是否可以加床：</span>
              <el-select
                v-model="roominfolist.fjBed"
                placeholder="选择是否可以加床"
                style="width: 400px;"
              >
                <el-option
                  v-for="item in optionsbad"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-col>
        </el-row>

        <!-- <el-form-item label="房间类型：" prop="cateIds">
          <el-input
            v-model="roominfolist.fjType"
            maxlength="120"
            placeholder="请输入房间类型"
          />
        </el-form-item> -->

        <span class="textarea">
          <el-form-item label="备注：" prop="remark">
            <span>
              <el-input
                type="textarea"
                v-model="roominfolist.remark"
                maxlength="240"
                placeholder="请输入备注"
              />
            </span>
          </el-form-item>
        </span>
        <!-- 房间（图片） -->
        <div class="upload">
          <el-form-item label="房间图片：" prop="sliderImages">
            <div class="acea-row">
              <div
                v-for="(item, index) in roominfolist.fjImage"
                :key="index"
                class="pictrue"
                draggable="true"
              >
                <img :src="item" class="img" />
                <i
                  v-if="!isDisabled"
                  class="el-icon-error btndel"
                  @click="handleRemove(index)"
                />
              </div>
              <div
                v-if="roominfolist.fjImage.length < 10 && !isDisabled"
                class="upLoadPicBox"
                @click="modalPicTap('2')"
              >
                <div class="upLoad">
                  <i class="el-icon-camera cameraIconfont" />
                </div>
              </div>
            </div>
          </el-form-item>
        </div>

        <!-- 室外（图片） -->
        <div class="upload">
          <el-form-item label="室外图片：" prop="sliderImages">
            <div class="acea-row">
              <div
                v-for="(item, index) in roominfolist.outdoor"
                :key="index"
                class="pictrue"
                draggable="true"
              >
                <img :src="item" class="img" />
                <i
                  v-if="!isDisabled"
                  class="el-icon-error btndel"
                  @click="handleRemovetow(index)"
                />
              </div>
              <div
                v-if="roominfolist.outdoor.length < 10 && !isDisabled"
                class="upLoadPicBox"
                @click="modalPicTa('2')"
              >
                <div class="upLoad">
                  <i class="el-icon-camera cameraIconfont" />
                </div>
              </div>
            </div>
          </el-form-item>
        </div>
        <div style="margin-left: 120px">
          <el-button type="primary" class="submission" @click="eadd(roomid)"
            >确定</el-button
          >
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  addRoominfoilApi,
  eidtRoominfoilApi,
  RoominfoDetailApi
} from "@/api/roomtypeInfo";
import { groggeryListApi } from "@/api/groggery";
const defaultObj = {
  image: "",
  sliderImages: [],
  videoLink: "",
  sliderImage: "",
  storeName: "",
  storeInfo: "",
  keyword: "",
  cateIds: [], // 商品分类id
  cateId: null, // 商品分类id传值
  unitName: "",
  sort: 0,
  giveIntegral: 0,
  ficti: 0,
  isShow: false,
  isBenefit: false,
  isNew: false,
  isGood: false,
  isHot: false,
  isBest: false,
  tempId: "",
  attrValue: [
    {
      image: "",
      price: 0,
      cost: 0,
      otPrice: 0,
      stock: 0,
      barCode: "",
      weight: 0,
      volume: 0
    }
  ],
  attr: [],
  selectRule: "",
  isSub: false,
  content: "",
  specType: false,
  id: 0,
  couponIds: [],
  coupons: [],
  activity: ["默认", "秒杀", "砍价", "拼团"]
};
export default {
  data() {
    return {
      //室外景观图
      formValidate: Object.assign({}, defaultObj),
      isDisabled: this.$route.params.isDisabled === "1" ? true : false,
      // 是否可以加床
      optionsbad: [
        {
          value: 0,
          label: "不可以加床"
        },
        {
          value: 1,
          label: "可以加床"
        }
      ],
      //验证规则
      rules: {
        //房间类型
        fjType: [
          { required: true, message: "请输入房间类型", trigger: "blur" }
        ],
        //饮品
        drink: [{ required: true, message: "请输饮品", trigger: "blur" }],
        //房间设施
        facility: [
          { required: true, message: "请输入房间设施", trigger: "blur" }
        ],
        //房间面积：
        fjArea: [
          { required: true, message: "请输入房间面积", trigger: "blur" }
        ],
        //房间密码
        fjPwd: [{ required: true, message: "请输入房间密码", trigger: "blur" }],
        //商品床型
        fjBedType: [
          { required: true, message: "请输入商品床型", trigger: "blur" }
        ],
        //卫生间信息
        fjToilet: [
          { required: true, message: "请输入卫生间信息", trigger: "blur" }
        ],
        //房型名称
        fxName: [
          { required: true, message: "请选择房型名称", trigger: "blur" }
        ],
        //房间窗户信息
        fjWindows: [
          { required: true, message: "请输入房间窗户信息", trigger: "blur" }
        ],
        //容纳人数
        galleryful: [
          { required: true, message: "请输入容纳人数", trigger: "blur" }
        ],
        //洗漱用品
        wash: [{ required: true, message: "请输入洗漱用品", trigger: "blur" }],
        //备注
        remark: [{ required: true, message: "请填写备注", trigger: "blur" }],
        recreation: [
          { required: true, message: "请输入娱乐设备", trigger: "blur" }
        ]
      },
      //eadd数据源
      roominfolist: {
        fjImage: [], //房间图片
        createTime: "", //创建时间
        drink: "", //饮品
        facility: "", //房间设施
        fjArea: "", //房间面积
        fjBed: "", //是否可以加床(1可以)
        fjBedType: "", //床型
        fjFloor: "", //楼层
        fjToilet: "", //房间卫生间信息
        fjType: "", //房间类型
        fjWindows: "", //房间窗户信息
        fxName: "", //房型名称
        galleryful: "", //容纳人数
        id: 0, //房间信息表id标识
        isDel: 0, //是否删除
        jdId: "", //酒店id
        outdoor: [], //室外景观（图片）
        recreation: "", //娱乐设备
        remark: "", //备注
        sort: "", //排序
        updateTime: "", //更新时间
        wash: "" //洗漱用品
      },
      //传过来的id
      roomid: "",
      pagesize: {
        // 每页数量
        limit: 20,
        // 页码
        page: 1
      },
      //查询所有酒店
      hotalForm: {
        limit: 10,
        page: 1
      },
      //选择酒店
      options: [],
      value: ""
    };
  },
  created() {
    this.gethotalList();
  },
  mounted() {
    if (this.$route.params.id) {
      this.roomid = this.$route.params.id;
      this.loadData(this.$route.params.id);
    }
  },
  methods: {
    //上传组件
    handleRemove(i) {
      this.roominfolist.fjImage.splice(i, 1);
    },
    handleRemovetow(i){
      this.roominfolist.outdoor.splice(i, 1);
    },
    //添加修改
    async eadd(roomid) {
      this.$refs.formValidate.validate(async valid => {
        if (!valid) return;
        // 修改
        if (roomid) {
          console.log(this.roominfolist, "001");
          await eidtRoominfoilApi(this.roominfolist);
          this.$message.success("修改成功");
        } else {
          // 新增
          await addRoominfoilApi(this.roominfolist);
          this.$message.success("新增成功");
        }
        this.$router.push("/amszs/roomInformation/index");
      });
    },
    // 返回
    back() {
      this.$router.go(-1);
    },
    // 根据id查渲染
    async loadData(roomid) {
      const res = await RoominfoDetailApi({ id: roomid });
      this.roominfolist = res;
      console.log("roominfolist", this.roominfolist);
    },
    // 获取所有的酒店
    async gethotalList() {
      const res = await groggeryListApi(this.hotalForm);
      this.options = res.list;
    },
    seletjd(id) {
      this.roominfolist.jdId = id;
    },
    // 点击商品图
    modalPicTap(tit, num, i, status) {
      const _this = this;
      if (_this.isDisabled) return;
      this.$modalUpload(
        function(img) {
          if (tit === "1" && !num) {
            _this.roominfolist.fjImage = img[0].sattDir;
            _this.OneattrValue[0].image = img[0].sattDir;
          }
          if (tit === "2" && !num) {
            if (img.length > 10)
              return this.$message.warning("最多选择10张图片！");
            if (img.length + _this.roominfolist.fjImage.length > 10)
              return this.$message.warning("最多选择10张图片！");
            img.map(item => {
              _this.roominfolist.fjImage.push(item.sattDir);
            });
          }
        },
        tit,
        "content"
      );
    },
    // 点击商品图
    modalPicTa(tit, num, i, status) {
      const _this = this;
      if (_this.isDisabled) return;
      this.$modalUpload(
        function(img) {
          if (tit === "1" && !num) {
            _this.roominfolist.outdoor = img[0].sattDir;
            _this.OneattrValue[0].image = img[0].sattDir;
          }
          if (tit === "2" && !num) {
            if (img.length > 10)
              return this.$message.warning("最多选择10张图片！");
            if (img.length + _this.roominfolist.outdoor.length > 10)
              return this.$message.warning("最多选择10张图片！");
            img.map(item => {
              _this.roominfolist.outdoor.push(item.sattDir);
            }); 
          }
        },
        tit,
        "content"
      );
    }
  }
};
</script>

<style lang="scss" scoped>
.eadd {
  padding: 20px;
  .fjBed {
    font-weight: 700;
    margin-left: 23px;
    margin-bottom: 20px;
    span {
      margin-right: 10px;
    }
  }
  .textarea {
    .el-form-item :last-child {
      width: 400px;
    }
  }

  .title {
    font-weight: 700;
    font-size: 20px;
  }
  .frame {
    .el-form-item {
      .el-input {
        width: 400px;
      }
      .el-select {
        width: 70px;
      }
    }
  }
  .upload {
    display: flex;
    width: 800px;
    height: 100px;
    .pictrue {
      width: 60px;
      height: 60px;
      border: 1px dotted rgba(0, 0, 0, 0.1);
      margin-right: 10px;
      position: relative;
      cursor: pointer;
    }
    .img {
      width: 60px;
      height: 60px;
    }
    .btndel {
      position: absolute;
      z-index: 11111;
      width: 20px !important;
      height: 20px !important;
      left: 46px;
      top: -4px;
    }
  }
}
</style>
